library(doBy)

load_it <- 1
if (load_it>0) {
    load("data/kff_main_replication_data_with_psraid.RData")
    load_it <- load_it+1
    }



sdat$INSURANCETYPE <- "Other"
sdat$INSURANCETYPE[sdat$MEDICAID==1] <- "Medicaid"
sdat$INSURANCETYPE[sdat$MEDICARESR==1] <- "Medicare"
sdat$INSURANCETYPE[sdat$EMPLINSURE==1] <- "Employer"
sdat$INSURANCETYPE[sdat$SELFINSURE==1] <- "Purchased"
sdat$INSURANCETYPE[sdat$COVERED==0] <- "None"

mean.na <- function(x){
	mean(x,na.rm=T)
}

load_it2 <- 1
if (load_it>0) {
    source("code/health_survey_load_helped_hurt.R")
    load_it2 <- load_it2+1
    sdat <- merge(sdat, kff_helped_hurt, by=c("PSRAID","NUMBER"), all.x=T)
    sdat$helped <- sdat$helped == "1"
    sdat$hurt <- sdat$hurt == "1"
    }

### 4 = FEB09
###

colr <- TRUE
helped_hurt <- "hurt"
if(colr==T){
	flname.jpg <- paste0("figs/",helped_hurt,"-by-insurance-by-time-smoothed-color-20200126-wh.jpg")
	flname.pdf <- paste0("figs/figureA5_",helped_hurt,"-by-insurance-by-time-smoothed-color-20200126-wh.pdf")
}else{
	flname.jpg <- paste0("figs/",helped_hurt,"-by-insurance-by-time-smoothed-gray-20200126-wh.jpg")
	flname.pdf <- paste0("figs/",helped_hurt,"-by-insurance-by-time-smoothed-gray-20200126-wh.pdf")
}

pdf(flname.pdf,width=10, height=3.75)

par(mfcol=c(1,2), mar=c(4,6,3,1))

gry.colr <- rep(gray(0.6), 5)

if (helped_hurt == "helped") {
    sdat$FAVORB <- sdat$helped
} else if (helped_hurt == "hurt") {
    sdat$FAVORB <- sdat$hurt
}

y.lower <- 0
y.higher <- 0.9

cex.txt.labels <- 1
cex.axt.labels <- 1.5

xmx <- 140

sout <- summaryBy(FAVORB ~ INSURANCETYPE*DATN,FUN=c(mean.na),data=sdat)
sout2 <- summaryBy(FAVORB ~ MARKET*DATN,FUN=c(mean.na),data=subset(sdat, !is.na(MARKET) & !is.na(FAVORB) ))

xmin <- sout$DATN[sout$INSURANCETYPE=="Employer"][min(which(!is.na(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Employer"])))]
xmax <- sout$DATN[sout$INSURANCETYPE=="Employer"][max(which(!is.na(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Employer"])))]+1

smout <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Employer"] ~ sout$DATN[sout$INSURANCETYPE=="Employer"],span=.2)
pout <- predict(smout,sout$DATN[sout$INSURANCETYPE=="Employer"])
plot(y=pout,x=sout$DATN[sout$INSURANCETYPE=="Employer"],type="l",pch=16,
	ylim=c(y.lower,y.higher),xlim=c(xmin,xmax),xlab="Year",ylab=paste0("Percent Reporting ", ifelse(helped_hurt=="helped", "Benefit\n", "Harm\n")),xaxt="n",cex.main=1.2,cex.lab=1.5,yaxt="n",cex=.5,lwd=4, bty="n", main = "") #, mgp=c(3.5,1,0)

text(ifelse(helped_hurt=="helped", "Employer-Provided", "Employer-\nProvided"),x=ifelse(helped_hurt=="helped", 45, 58),y=max(pout, na.rm=T) + ifelse(helped_hurt=="helped", -0.125, 0.1),col="black",cex=cex.txt.labels)

ss <- seq(from=1,to=132,by=12)
lbs <- c("09","10","11","12","13","14","15","16","17","18","19")
axis(lty=0,line=NA,tick="no",at=c(ss),labels=lbs,side=1,cex.lab=1.8,cex=cex.axt.labels,cex.axis=cex.axt.labels)
axis(lty=0,line=NA,tick="no",at=c(0,.25,.5,.75,1),labels=c("0","25","50","75","100"),side=2,cex.lab=1.5,cex=cex.axt.labels,cex.axis=cex.axt.labels, las=2)


par(new=T)

smout2 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Medicaid"] ~ sout$DATN[sout$INSURANCETYPE=="Medicaid"],span=.15)
pout2 <- predict(smout2,sout$DATN[sout$INSURANCETYPE=="Medicaid"])
if(colr==T){
	lines(y=pout2,x=sout$DATN[sout$INSURANCETYPE=="Medicaid"],type="l",pch=18,
             ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col="blue",yaxt="n",cex=.5,lwd=4,
             bty="n"
             )
        abline(h=c(0,1))
	text("Medicaid",x=53,y=max(pout2, na.rm=T)+ifelse(helped_hurt=="helped", 0.05, -0.15),col="blue",cex=cex.txt.labels)


}else{
		lines(y=pout2,x=sout$DATN[sout$INSURANCETYPE=="Medicaid"],type="l",pch=18,
                     ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col=gry.colr[2],yaxt="n",cex=.5,lwd=4,
                     bty="n")
                abline(h=c(0,1))
		text("Medicaid",x=53,y=max(pout2, na.rm=T)+ifelse(helped_hurt=="helped", 0.05, -0.15),col=gry.colr[2],cex=cex.txt.labels)
}

par(new=T)

smout4 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Medicare"] ~ sout$DATN[sout$INSURANCETYPE=="Medicare"],span=.15)
pout4 <- predict(smout4,sout$DATN[sout$INSURANCETYPE=="Medicare"])
lines(y=pout4,x=sout$DATN[sout$INSURANCETYPE=="Medicare"],type="l",pch=20,
	ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col=gry.colr[3],yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
text("Medicare",x=50,y=max(pout4, na.rm=T) + ifelse(helped_hurt=="helped", 0.05, -0.18),col=gray(0.3),cex=cex.txt.labels)

abline(v=12*5+1,lty=2)
mtext("Exchanges Open",at=12*5)


smout3 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="None"] ~ sout$DATN[sout$INSURANCETYPE=="None"],span=.15)
pout3 <- predict(smout3,sout$DATN[sout$INSURANCETYPE=="None"])
plot(y=pout3,x=sout$DATN[sout$INSURANCETYPE=="None"],type="l",pch=15,
	ylim=c(y.lower,y.higher),xlim=c(xmin,xmax),xaxt="n",col=gry.colr[3],
    ,xlab="Year",ylab="",xaxt="n",cex.main=1.2,cex.lab=1.5,yaxt="n",cex=.5,lwd=4, bty="n", main = "")
axis(lty=0,line=NA,tick="no",at=c(0,.25,.5,.75,1),labels=c("0","25","50","75","100"),side=2,cex.lab=1.5,cex=cex.axt.labels,cex.axis=cex.axt.labels, las=2)
abline(h=c(0,1))
ss <- seq(from=1,to=132,by=12)
lbs <- c("09","10","11","12","13","14","15","16","17","18","19")

axis(lty=0,line=NA,tick="no",at=c(ss),labels=lbs,side=1,cex.lab=1.5,cex=cex.axt.labels,cex.axis=cex.axt.labels)

text("Uninsured",x=60,y=ifelse(helped_hurt=="helped", 0.05, max(pout3, na.rm=T)+0.15),col=gray(0.4),cex=cex.txt.labels)

par(new=T)

smout1 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Purchased"] ~ sout$DATN[sout$INSURANCETYPE=="Purchased"],span=.15)
pout1 <- predict(smout1,sout$DATN[sout$INSURANCETYPE=="Purchased"])
if(colr==T){
	lines(y=pout1,x=sout$DATN[sout$INSURANCETYPE=="Purchased"],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col="orange4",yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Purchased",x=ifelse(helped_hurt=="helped", 59, 48),y=max(pout1, na.rm=T)+ifelse(helped_hurt=="helped", 0.075, -0.05),col="orange4",cex=cex.txt.labels)

}else{
	lines(y=pout1,x=sout$DATN[sout$INSURANCETYPE=="Purchased"],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col=gry.colr[1],yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Purchased",x=ifelse(helped_hurt=="helped", 59, 48),y=pout1[max(pout1, na.rm=T)]+ifelse(helped_hurt=="helped", 0.075, -0.05),col=gry.colr[1],cex=cex.txt.labels)
}


smout1 <- loess(sout2$FAVORB.mean.na[sout2$MARKET==1] ~ sout2$DATN[sout2$MARKET==1],span=.5)
pout1 <- sout2$FAVORB.mean.na[sout2$MARKET==1]
if(colr==T){
	lines(y=pout1,x=sout2$DATN[sout2$MARKET==1],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col="#7b3294",yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Used\nExchanges",x=ifelse(helped_hurt=="helped", 55, 53),y=ifelse(helped_hurt=="helped", pout1[length(pout1)]-0.05, 0.1),col="#7b3294",cex=cex.txt.labels)

}else{
	lines(y=pout1,x=sout2$DATN[sout2$MARKET==1],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(xmin, xmax),xlab="",ylab="",xaxt="n",col=gry.colr[1],yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Used\nExchanges",x=ifelse(helped_hurt=="helped", 55, 53),y=ifelse(helped_hurt=="helped", pout1[length(pout1)]-0.05, 0.1),col=gry.colr[1],cex=cex.txt.labels)
}

abline(v=12*5+1,lty=2)
mtext("Exchanges Open",at=12*5)

mtext(ifelse(helped_hurt=="helped", "    Benefited from ACA", "    Negatively Affected by ACA"), side = 3, line = -1.5, outer = TRUE, cex=1.4, font=2)

dev.off()

#### Table A2
aggregate(!is.na(hurt) ~ INSURANCETYPE, subset(sdat, DATN>60.5), FUN = sum)
aggregate(!is.na(hurt) ~ MARKET, subset(sdat, DATN>60.5), FUN = sum)
